package com.meihua.shellapp.base;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.aspsine.irecyclerview.OnLoadMoreListener;
import com.aspsine.irecyclerview.OnRefreshListener;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.bean.BannerBean;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.utils.LogUtils;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${zhangyuanchao} on 2017/9/27.
 */

public abstract class BaseRecyclerViewFragment1 extends BaseFragment {
    public LoadMoreFooterView mLoadMoreFooterView;
    private RecyclerView.Adapter mAdapter;
    public int mPage = 1;
    IRecyclerView recyclerView;
    public TextView bannerTv;

    /*初始化irecyclerview*/
    public void initRecyclerView(final IRecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        /*加载更多底部View*/
        mLoadMoreFooterView = (LoadMoreFooterView) recyclerView.getLoadMoreFooterView();
        if (mAdapter == null) {
            mAdapter = getAdapter();
            recyclerView.setIAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
        recyclerView.setOnRefreshListener(onRefreshListener);
        recyclerView.setOnLoadMoreListener(onLoadMoreListener);
    }

    @Override
    public void loadData() {
        super.loadData();
        getServiceData(1);
    }

    /*设置binnerView*/
    public void setBannerView(final SliderLayout slider, String url) {
        slider.removeAllSliders();
        /*获取banner数据*/
        Map<String, String> params = new HashMap() {{
        }};
        NetHelper.post(url, params, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                BannerBean bean = MyApplication.getInstance().getBeanForJson(BannerBean.class, response.body());
                if (bean == null) return;
                List<BannerBean.DataBean> data = bean.getData();
                parserData(data, slider);
            }
        }, true);

    }

    private void parserData(final List<BannerBean.DataBean> data, SliderLayout slider) {
        for (int i = 0; i < data.size(); i++) {
            //显示图片和描述文本的控件
            TextSliderView textSliderView = new TextSliderView(getActivity());
            textSliderView
                    .empty(R.drawable.banner_place)
                    .error(R.drawable.banner_place)
                    .image(URL.getUrl().HOST + data.get(i).getLogo())//指定图片链接地址
                    .setScaleType(BaseSliderView.ScaleType.CenterCrop);//指定图片展示方式
            //将textSliderView放置到容器中
            slider.addSlider(textSliderView);
            final int finalI = i;
            textSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                @Override
                public void onSliderClick(BaseSliderView slider) {
                    startActivity(new Intent(getActivity(), WebActivity.class).putExtra("url", data.get(finalI).getLink()));

                }
            });

        }
    }

    protected abstract RecyclerView.Adapter getAdapter();

    protected abstract void getServiceData(int mPage);

    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            mPage++;
            getServiceData(mPage);
            mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
        }
    };


    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPage = 1;
            recyclerView.setRefreshing(true);
            getServiceData(mPage);
            mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);
        }
    };

}
